<!DOCTYPE html>
<html>
  <head>
  	<div th:include="base-libs :: base-libs" />
    <title>添加关联机构</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
	<script>
		var hygzglData = StoreCache.removeCache('hygzglData');
		$(function(){
			initView();
			initData();
		})
		
		function initData(){
			$("#btnCx").trigger("click");
		}
		
		function initView(){
			//查询
			$("#btnCx").click(function(){
				$("#yljgTb").datagrid({url: "/console/yljgxx/queryPageSummary.service"});
			})
			
			//确认
			$("#btnQr").click(function(){
				var selects = $("#yljgTb").datagrid('getSelections');
				if(selects.length>0){
					var gljgArr = [];
					$.each(selects, function(){
						var param = {
							PTJGID: this.PTJGID,
							YLJGDM: this.YLJGDM,
							YLJGMC: this.YLJGMC
						}
						gljgArr.push(param);
					})
					$.QuickRemote.AjaxJson("saveGljg", ajaxHandler, {gljg: JSON.stringify(gljgArr),GZBH: hygzglData.GZBH});
				}else{
					$.QuickAlert.alertFail({content: '请至少选择一个医疗机构！'});
				}
			})
			
			//取消
			$("#btnQx").click(function(){
				parent.layer.closeAll();
			})
			
			//医疗机构table
			$("#yljgTb").datagrid({
				idField : "PTJGID",
				striped : true,
				clickToSelect: true,
				fitColumns: true,
				fit:true,
				scrollbarSize:15,
				columns : [[
					{field:'xz',checkbox:true},   
					{field:'YLJGMC',title:'机构名称',width:240},
	                {field:'XZQHMC',title:'行政区划',width:80},
	                {field:'WSJGLBMC',title:'机构类别',width:80},
	                {field:'YYDJMC',title:'机构等级',width:80}
				 ]],
				pagination : true,
				pageList: [10, 20, 50, 100, 200],
				onBeforeLoad : function(params) {
					var url = $(this).datagrid("options")["url"];
					if(!StringUtils.isEmpty(url)){
						params["pageSize"] = params["rows"];
						params["pageNumber"] = params["page"];
						params["YLJGMC"] = $("#YLJGMC").val();
						//params["not_PTJGID"] = hygzglData.PTJGID;
					}else{
						return false;
					}
				},
				onLoadSuccess: function(data){
// 					alert(JSON.stringify(data))

					//删除当前记录对应的机构(本机构)
					var delIndex = $("#yljgTb").datagrid('getRowIndex', hygzglData.PTJGID);//通过主键id获取本机构对应的下标
					if(delIndex != -1){
						$("#yljgTb").datagrid('deleteRow', delIndex);
					}
					//选中原纪录
					checkedTr();
 				},
			});
		}
		
		/** ajax回调 */
		function ajaxHandler(result){
			if(result.state){
				$.QuickAlert.alertNormal(result.info, {callback: function(){
					StoreCache.setCache("refresh",true);
					$("#btnQx").trigger("click");
				}});
			}else{
				$.QuickAlert.alertFail({content: result.info});
			}
		}
		
		/** 选中原纪录tr */
		var thisGljg;
		function checkedTr(){
			if($.isEmptyObject(thisGljg)){
				$.QuickRemote.AjaxJson("queryGljg", function(result){
					if(result.state){
						if(result.rows.length>0){
							thisGljg = result.rows; 
							checkedTr();
						}
					}else{
						$.QuickAlert.alertFail({content: result.info});
					}
				}, {GZBH: hygzglData.GZBH});
			}else{
				$.each(thisGljg, function(){
					//勾选
					$("#yljgTb").datagrid('selectRecord', this.PTJGID);
				})
			}
		}
	</script>
  </head>
  
  <body class="easyui-layout">
    <div region="north" style="overflow:hidden;height:50px;line-height:50px;border:0;border-bottom:1px solid #ddd">
    	<table class="form-table" id="conditionBox">
			<tr>
				<td class="td-text">机构名称：</td>
				<td class="td-content">
					<input id="YLJGMC" />
				</td>
				<td class="td-text" style="text-align:center">
					<button id="btnCx" type="button">查询</button>
				</td>
			</tr>
		</table>
    </div>
    <div region="center" border="false">
    	<table id="yljgTb" border="false"></table>
    </div>
    <div region="south" style="height:50px;line-height:50px;text-align:center;overflow:hidden;border:0;border-top:1px solid #ddd">
    	<button id="btnQr" type="button">确认</button>
		<button id="btnQx" type="button">取消</button>
    </div>
  </body>
</html>
